package com.bootdo.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.system.domain.UserDO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.WithdrawDO;
import com.bootdo.system.service.WithdrawService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 体现申请表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021/02/05 16:06:00
 */
 
@Controller
@RequestMapping("/system/withdraw")
public class WithdrawController {
	@Autowired
	private WithdrawService withdrawService;
	
	@GetMapping()
	@RequiresPermissions("system:withdraw:withdraw")
	String Withdraw(){
	    return "system/withdraw/withdraw";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:withdraw:withdraw")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		UserDO userDO =  (UserDO)SecurityUtils.getSubject().getPrincipal();
		params.put("userId",userDO.getUserId());
        Query query = new Query(params);
		List<WithdrawDO> withdrawList = withdrawService.list(query);
		int total = withdrawService.count(query);
		PageUtils pageUtils = new PageUtils(withdrawList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "system/withdraw/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		WithdrawDO withdraw = withdrawService.get(id);
		model.addAttribute("withdraw", withdraw);
	    return "system/withdraw/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( WithdrawDO withdraw){
		UserDO userDO =  (UserDO)SecurityUtils.getSubject().getPrincipal();
		withdraw.setUserId(userDO.getUserId());
		withdraw.setStatus(0);
		withdraw.setCreatedAt(new Date());
		withdraw.setCreatedBy(userDO.getUserId());
		if(userDO.getBalance()==null||"".equals(userDO.getBalance())){
			return R.error("账户信息异常");
		}
		if(Double.valueOf(userDO.getBalance())-withdraw.getNumber()<0.01){
			return R.error("体现金额不能大于账户余额");
		}
		if(withdrawService.save(withdraw)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( WithdrawDO withdraw){
		withdrawService.update(withdraw);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long id){
		if(withdrawService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		withdrawService.batchRemove(ids);
		return R.ok();
	}

	@GetMapping("to_index")
	@RequiresPermissions("system:withdraw:withdraw:audit")
	String index(){
		return "system/withdraw/index";
	}

	@ResponseBody
	@GetMapping("/index")
	@RequiresPermissions("system:withdraw:withdraw:audit")
	public PageUtils index(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<WithdrawDO> withdrawList = withdrawService.list(query);
		int total = withdrawService.count(query);
		PageUtils pageUtils = new PageUtils(withdrawList, total);
		return pageUtils;
	}

	/**
	 * 审核发放
	 * @param id
	 * @return
	 */
	@PostMapping( "/audit")
	@ResponseBody
	public R audit( Long id){

		UserDO userDO =  (UserDO)SecurityUtils.getSubject().getPrincipal();
		if(withdrawService.audit(id,userDO.getUserId())){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 审核拒绝
	 * @param id
	 * @return
	 */
	@PostMapping( "/refuse")
	@ResponseBody
	public R refuse( Long id){
		UserDO userDO =  (UserDO)SecurityUtils.getSubject().getPrincipal();
		if(withdrawService.refuse(id,userDO.getUserId())){
			return R.ok();
		}
		return R.error();
	}


}
